<!DOCTYPE html>

<html>
<head>
  <title>base.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div class="container">
    <div class="page">

      <div class="header">
        
          <h1>base.coffee</h1>
        

        
          <div class="toc">
            <h3>Table of Contents</h3>
            <ol>
              
                
                <li>
                  <a class="source" href="admin.html">
                    admin.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.auditData.html">
                    api.auditData.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.auditEvent.html">
                    api.auditEvent.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.html">
                    api.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.entity.html">
                    api.entity.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.map.html">
                    api.map.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.server.html">
                    api.server.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.variable.html">
                    api.variable.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.view.html">
                    api.view.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="app.instance.html">
                    app.instance.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="appjs.html">
                    appjs.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="data.html">
                    data.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="dataUtil.html">
                    dataUtil.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="manager.html">
                    manager.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="messages.html">
                    messages.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditData.html">
                    auditData.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditEvent.html">
                    auditEvent.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="base.html">
                    base.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityDefinition.html">
                    entityDefinition.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityObject.html">
                    entityObject.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="eventAction.html">
                    eventAction.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="eventRule.html">
                    eventRule.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="link.html">
                    link.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="map.html">
                    map.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shape.html">
                    shape.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="user.html">
                    user.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="userSettings.html">
                    userSettings.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="variable.html">
                    variable.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="adminRoutes.html">
                    adminRoutes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="appRoutes.html">
                    appRoutes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="settings.html">
                    settings.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sockets.html">
                    sockets.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="tutorial.html">
                    tutorial.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="vectors.html">
                    vectors.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="statusTabView.html">
                    statusTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="toolsTabView.html">
                    toolsTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="usersTabView.html">
                    usersTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="adminView.html">
                    adminView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="alertView.html">
                    alertView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditDataManagerView.html">
                    auditDataManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditEventManagerView.html">
                    auditEventManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="baseView.html">
                    baseView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="createMapView.html">
                    createMapView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityManagerView.html">
                    entityManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="footerView.html">
                    footerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="helpView.html">
                    helpView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entitiesTabView.html">
                    entitiesTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="inspectorTabView.html">
                    inspectorTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="mapTabView.html">
                    mapTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeTabView.html">
                    shapeTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="controlsView.html">
                    controlsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="labelEditView.html">
                    labelEditView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkCreatorView.html">
                    linkCreatorView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkLabelsView.html">
                    linkLabelsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkView.html">
                    linkView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeLabelsView.html">
                    shapeLabelsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeView.html">
                    shapeView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapesMoverView.html">
                    shapesMoverView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="mapView.html">
                    mapView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="menuView.html">
                    menuView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="overlayView.html">
                    overlayView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="scriptEditorView.html">
                    scriptEditorView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="settingsView.html">
                    settingsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="startView.html">
                    startView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="variableManagerView.html">
                    variableManagerView.coffee
                  </a>
                </li>
              
            </ol>
          </div>
        
      </div>

      
        
        <h2 id="base-model">BASE MODEL</h2>

        
      
        
        <p>This is the base abstract model for all models of the System App.</p>

        
          <div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">SystemApp</span>.<span class="title">BaseModel</span> <span class="keyword">extends</span> <span class="title">Backbone</span>.<span class="title">Model</span></span>

    <span class="attribute">typeName</span>: <span class="literal">null</span></pre></div>
        
      
        
        <p>When was it last saved?</p>

        
          <div class='highlight'><pre>    <span class="attribute">lastSave</span>: <span class="keyword">new</span> Date(<span class="number">2000</span>, <span class="number">1</span>, <span class="number">1</span>)</pre></div>
        
      
        
        <p>If model is syncing with server, property fetching will be true.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetching</span>: <span class="literal">false</span></pre></div>
        
      
        
        <p>Used for delayed saving to the server, so user can change multiple
properties of the model and it will send the changes to the server
all at once after a few milliseconds.</p>

        
          <div class='highlight'><pre>    <span class="attribute">timerSave</span>: <span class="literal">null</span></pre></div>
        
      
        
        <h2 id="common-properties">COMMON PROPERTIES</h2>

        
      
        
        <p>Get or set the model&#39;s active property.</p>

        
          <div class='highlight'><pre>    <span class="attribute">active</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        <span class="keyword">if</span> value?
            <span class="property">@set</span> <span class="string">"active"</span>, value
        <span class="property">@get</span> <span class="string">"active"</span></pre></div>
        
      
        
        <p>Get or set the model&#39;s friendly ID (updateable by the user, and different from the fixed &quot;id&quot;).</p>

        
          <div class='highlight'><pre>    <span class="attribute">friendlyId</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        <span class="keyword">if</span> value?
            <span class="property">@set</span> <span class="string">"friendlyId"</span>, value

        result = <span class="property">@get</span> <span class="string">"friendlyId"</span>

        <span class="keyword">if</span> <span class="keyword">not</span> result? <span class="keyword">or</span> result <span class="keyword">is</span> <span class="string">""</span>
            result = SystemApp.DataUtil.normalize(<span class="property">@get</span>(<span class="string">"name"</span>), <span class="literal">true</span>)
            result = SystemApp.DataUtil.normalize(<span class="property">@get</span>(<span class="string">"title"</span>), <span class="literal">true</span>) <span class="keyword">if</span> <span class="keyword">not</span> result? <span class="keyword">or</span> result <span class="keyword">is</span> <span class="string">""</span>
            result = SystemApp.DataUtil.normalize(<span class="property">@get</span>(<span class="string">"id"</span>), <span class="literal">true</span>) <span class="keyword">if</span> <span class="keyword">not</span> result? <span class="keyword">or</span> result <span class="keyword">is</span> <span class="string">""</span>
            <span class="property">@set</span> <span class="string">"friendlyId"</span>, result

        <span class="keyword">return</span> result</pre></div>
        
      
        
        <h2 id="id-and-url-helpers">ID AND URL HELPERS</h2>

        
      
        
        <p>Set a new entity ID, based on current date and an extra 2 random characters.</p>

        
          <div class='highlight'><pre>    <span class="attribute">generateId</span>:<span class="function"> =&gt;</span>
        chars = <span class="string">"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"</span>
        today = <span class="keyword">new</span> Date()
        result = today.valueOf().toString <span class="number">16</span>
        result += chars.substr Math.floor(Math.random() * chars.length), <span class="number">1</span>
        result += chars.substr Math.floor(Math.random() * chars.length), <span class="number">1</span>

        <span class="property">@set</span> <span class="string">"id"</span>, result

        <span class="keyword">return</span> result</pre></div>
        
      
        
        <p>Return the model&#39;s ID to be used on the local storage.</p>

        
          <div class='highlight'><pre>    <span class="attribute">localId</span>:<span class="function"> =&gt;</span>
        result = <span class="property">@typeName</span>
        result += <span class="property">@id</span> <span class="keyword">if</span> <span class="property">@id</span>?
        <span class="keyword">return</span> result</pre></div>
        
      
        
        <p>The URL to call when syncing with server. If the model&#39;s ID is set, then
append it to the end of the URL.</p>

        
          <div class='highlight'><pre>    <span class="attribute">url</span>:<span class="function"> =&gt;</span>
        t = SystemApp.Settings.general.baseJsonUrl + <span class="property">@typeName</span>.toLowerCase()

        <span class="keyword">if</span> <span class="property">@id</span>? <span class="keyword">and</span> <span class="property">@id</span> <span class="keyword">isnt</span> <span class="string">""</span>
            <span class="keyword">return</span> t + <span class="string">"/"</span> + <span class="property">@id</span>

        <span class="keyword">return</span> t</pre></div>
        
      
        
        <h2 id="parse-and-tojson">PARSE AND TOJSON</h2>

        
      
        
        <p>Initialize the model, make sure its nested models and collections are created.</p>

        
          <div class='highlight'><pre>    <span class="attribute">initialize</span>:<span class="function"> =&gt;</span>
        <span class="keyword">return</span> <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@relations</span>?

        <span class="keyword">for</span> key <span class="keyword">of</span> <span class="property">@relations</span>
            <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@attributes</span>[key]?.typeName?
                <span class="property">@attributes</span>[key] = <span class="keyword">new</span> <span class="property">@relations</span>[key](<span class="property">@attributes</span>[key])
                <span class="property">@attributes</span>[key].parentModel = <span class="keyword">this</span></pre></div>
        
      
        
        <p>If model has <code>relations</code> set, transform the data to proper models or collections.</p>

        
          <div class='highlight'><pre>    <span class="attribute">parse</span>: <span class="function"><span class="params">(response)</span> =&gt;</span>
        <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@relations</span>?
            <span class="keyword">return</span> response

        <span class="keyword">for</span> key <span class="keyword">of</span> <span class="property">@relations</span>
            current = <span class="property">@get</span> key
            responseValue = response[key]</pre></div>
        
      
        
        <p>Make sure value is valid JSON and not a string. Remove last <code>;</code> if there&#39;s one.
Alert if the response can&#39;t be parsed.</p>

        
          <div class='highlight'><pre>            <span class="keyword">if</span> <span class="keyword">typeof</span> responseValue <span class="keyword">is</span> <span class="string">"string"</span>
                <span class="keyword">try</span>
                    <span class="keyword">if</span> responseValue.substring(responseValue.length - <span class="number">1</span>) <span class="keyword">is</span> <span class="string">";"</span>
                        responseValue = responseValue.substring(<span class="number">0</span>, responseValue.length - <span class="number">1</span>)
                    responseValue = JSON.parse responseValue
                <span class="keyword">catch</span> ex
                    console.warn <span class="property">@typeName</span> + <span class="string">".parse"</span>, responseValue, ex</pre></div>
        
      
        
        <p>If the current attribute is already &quot;set&quot; to the defined model / collection,
then update its value. Otherwise create a new model / collection.</p>

        
          <div class='highlight'><pre>            <span class="keyword">if</span> current?.typeName?
                <span class="keyword">if</span> responseValue?.models?
                    current.set responseValue.models
                <span class="keyword">else</span>
                    current.set responseValue
                response[key] = current
            <span class="keyword">else</span>
                response[key] = <span class="keyword">new</span> <span class="property">@relations</span>[key](responseValue)
                response[key].parentModel = <span class="keyword">this</span>

        <span class="keyword">return</span> response</pre></div>
        
      
        
        <p>If model has <code>relations</code> set, transform sub models and collections to JSON.</p>

        
          <div class='highlight'><pre>    <span class="attribute">toJSON</span>:<span class="function"> =&gt;</span>
        result = _.clone <span class="property">@attributes</span>

        <span class="keyword">if</span> <span class="property">@relations</span>?
            <span class="keyword">for</span> key <span class="keyword">of</span> <span class="property">@relations</span>
                result[key] = result[key].toJSON() <span class="keyword">if</span> result[key]?

        <span class="keyword">return</span> result</pre></div>
        
      
        
        <h2 id="fetch-data">FETCH DATA</h2>

        
      
        
        <p>Override backbone&#39;s <code>fetch</code>. Set <code>fetching</code> to true before
fetching new data from server, and cancel fetching if collection
is being fetched already.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetch</span>: <span class="function"><span class="params">(options)</span> =&gt;</span>
        <span class="keyword">return</span> <span class="keyword">if</span> <span class="property">@fetching</span>
        <span class="property">@fetching</span> = <span class="literal">true</span>

        options = {} <span class="keyword">if</span> <span class="keyword">not</span> options?</pre></div>
        
      
        
        <p>Override <code>success</code> and <code>error</code>. If these callbacks are passed, they&#39;ll be handled
later inside <code>fetchSuccess</code> and <code>fetchError</code>.</p>

        
          <div class='highlight'><pre>        options.successCallback = options.success
        options.success = <span class="property">@fetchSuccess</span>
        options.errorCallback = options.error
        options.error = <span class="property">@fetchError</span>

        <span class="keyword">if</span> <span class="property">@attributes</span>.isLocal
            <span class="property">@fetchLocal</span> options
        <span class="keyword">else</span>
            Backbone.Model.prototype.fetch.call <span class="keyword">this</span>, options</pre></div>
        
      
        
        <p>Fetch data from local storage, using jquery.localData plugin.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetchLocal</span>: <span class="function"><span class="params">(options)</span> =&gt;</span>
        <span class="property">@fetching</span> = <span class="literal">true</span>

        options = {} <span class="keyword">if</span> <span class="keyword">not</span> options?
        options.isLocal = <span class="literal">true</span></pre></div>
        
      
        
        <p>Get local data.</p>

        
          <div class='highlight'><pre>        localObj = $.localData.get <span class="property">@localId</span>()
        <span class="property">@fetching</span> = <span class="literal">false</span></pre></div>
        
      
        
        <p>If local object exists and is parsed successfully, trigger <code>fetchSuccess</code>. # If failed, trigger <code>fetchError</code>.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> localObj?
            setResult = <span class="property">@set</span> <span class="property">@parse</span> localObj
            <span class="keyword">if</span> setResult?
                <span class="property">@fetchSuccess</span> <span class="keyword">this</span>, localObj, options
            <span class="keyword">else</span>
                <span class="property">@fetchError</span> <span class="keyword">this</span>, <span class="literal">null</span>, options
            <span class="property">@trigger</span> <span class="string">"sync"</span>, <span class="keyword">this</span>, localObj, options</pre></div>
        
      
        
        <p>When <code>fetch</code> is successful, set <code>fetching</code> to false.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetchSuccess</span>: <span class="function"><span class="params">(model, resp, options)</span> =&gt;</span>
        <span class="property">@fetching</span> = <span class="literal">false</span>

        options.successCallback? model, resp, options
        SystemApp.consoleLog <span class="property">@typeName</span> + <span class="string">".fetch"</span>, <span class="string">"Success "</span> + <span class="property">@id</span>, resp</pre></div>
        
      
        
        <p>When <code>fetch</code> has problems or throws an error, set <code>fetching</code> to false
and log to the console if debugging is enabled.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetchError</span>: <span class="function"><span class="params">(model, xhr, options)</span> =&gt;</span>
        <span class="property">@fetching</span> = <span class="literal">false</span>

        options.errorCallback? model, xhr, options
        SystemApp.consoleLog <span class="property">@typeName</span> + <span class="string">".fetch"</span>, <span class="string">"Error "</span> + <span class="property">@id</span>, xhr</pre></div>
        
      
        
        <h2 id="saving-data">SAVING DATA</h2>

        
      
        
        <p>Save model data. Try saving remotely, and if it fails, save a copy
on the local storage. Avoid saving to the server multiple times
in a row by using a timer to delay the remote save call.
Cancel the save is the model is currently being refreshed.</p>

        
          <div class='highlight'><pre>    <span class="attribute">save</span>: <span class="function"><span class="params">(key, val, options)</span> =&gt;</span>
        <span class="keyword">return</span> <span class="keyword">if</span> <span class="property">@fetching</span></pre></div>
        
      
        
        <p>We&#39;re not using <code>key</code> and <code>val</code> but Backbone uses it internally.
Just make sure <code>options</code> is properly set.</p>

        
          <div class='highlight'><pre>        options = val <span class="keyword">if</span> <span class="keyword">not</span> options?
        options = key <span class="keyword">if</span> <span class="keyword">not</span> options?

        <span class="keyword">if</span> <span class="property">@attributes</span>.isLocal
            <span class="property">@saveLocal</span> key, val, options
        <span class="keyword">else</span>
            <span class="keyword">if</span> <span class="property">@timerSave</span>?
                clearTimeout <span class="property">@timerSave</span>
                <span class="property">@timerSave</span> = <span class="literal">null</span>

            <span class="function"><span class="title">callback</span> = <span class="params">()</span> =&gt;</span> <span class="property">@saveRemote</span> options
            <span class="property">@timerSave</span> = setTimeout callback, SystemApp.Settings.general.saveInterval</pre></div>
        
      
        
        <p>Save model data to the local storage using jquery.localData plugin.</p>

        
          <div class='highlight'><pre>    <span class="attribute">saveLocal</span>: <span class="function"><span class="params">(key, val, options)</span> =&gt;</span>
        $.localData.set <span class="property">@localId</span>(), <span class="property">@toJSON</span>()
        options?.success? <span class="keyword">this</span>, <span class="property">@attributes</span>, options</pre></div>
        
      
        
        <p>Save model data to the remote server (Node / MongoDB).
If no options are passed, created one, and set the
<code>success</code> and <code>error</code> callbacks.</p>

        
          <div class='highlight'><pre>    <span class="attribute">saveRemote</span>: <span class="function"><span class="params">(options)</span> =&gt;</span>
        <span class="property">@timerSave</span> = <span class="literal">null</span>

        options = {} <span class="keyword">if</span> <span class="keyword">not</span> options?
        options.patch = SystemApp.Settings.general.savePatch</pre></div>
        
      
        
        <p>Override <code>success</code> and <code>error</code>. If these callbacks are passed, they&#39;ll be handled
later inside <code>saveSuccess</code> and <code>saveError</code>.</p>

        
          <div class='highlight'><pre>        options.successCallback = options.success
        options.success = <span class="property">@saveSuccess</span>
        options.errorCallback = options.error
        options.error = <span class="property">@saveError</span></pre></div>
        
      
        
        <p>If <code>options.patch</code> is set, then send only updated data to the server.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> options.patch <span class="keyword">and</span> <span class="keyword">not</span> <span class="property">@isNew</span>()

            attrs = <span class="property">@changed</span></pre></div>
        
      
        
        <p>Make sure changed attributes inside nested collections are passed along.
Important: this will only work for first level nesting!!!</p>

        
          <div class='highlight'><pre>            <span class="keyword">if</span> <span class="property">@relations</span>?
                <span class="keyword">for</span> key <span class="keyword">of</span> <span class="property">@relations</span></pre></div>
        
      
        
        <p>Get value of related model or collection.</p>

        
          <div class='highlight'><pre>                    propValue = <span class="property">@get</span> key</pre></div>
        
      
        
        <p>Has it a <code>changedModels</code> property?</p>

        
          <div class='highlight'><pre>                    <span class="keyword">if</span> propValue.changedModels
                        changedModels = propValue.changedModels()</pre></div>
        
      
        
        <p>Has changed models? If so, append to the <code>attrs</code>.</p>

        
          <div class='highlight'><pre>                        <span class="keyword">if</span> changedModels.length &gt; <span class="number">0</span>
                            attrs[key] = propValue.changedModels()</pre></div>
        
      
        
        <p>No patch, so send everything.</p>

        
          <div class='highlight'><pre>        <span class="keyword">else</span>
            attrs = {}

        Backbone.Model.prototype.save.call <span class="keyword">this</span>, attrs, options</pre></div>
        
      
        
        <p>Callback when the <code>save</code> is completed successfully. This will clear the local
storage data for this model, if there&#39;s one.</p>

        
          <div class='highlight'><pre>    <span class="attribute">saveSuccess</span>: <span class="function"><span class="params">(model, resp, options)</span> =&gt;</span>
        <span class="property">@lastSave</span> = <span class="keyword">new</span> Date()
        <span class="property">@deleteLocal</span>()

        options.successCallback? model, resp, options
        SystemApp.consoleLog <span class="property">@typeName</span> + <span class="string">".save"</span>, <span class="string">"Success "</span> + <span class="property">@id</span>, resp</pre></div>
        
      
        
        <p>Callback when the <code>save</code> is not completed successfully or had an error.</p>

        
          <div class='highlight'><pre>    <span class="attribute">saveError</span>: <span class="function"><span class="params">(model, xhr, options)</span> =&gt;</span>
        <span class="property">@saveLocal</span>()

        options.errorCallback? model, xhr, options
        SystemApp.consoleLog <span class="property">@typeName</span> + <span class="string">".save"</span>, <span class="string">"Error "</span> + <span class="property">@id</span>, xhr</pre></div>
        
      
        
        <p>Delete model data from local storage.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteLocal</span>:<span class="function"> =&gt;</span>
        $.localData.del <span class="property">@localId</span>()</pre></div>
        
      
        
        <h2 id="no-syncing-methods">NO SYNCING METHODS</h2>

        
      
        
        <p>To override the &quot;save&quot; method whenever we don&#39;t need to sync
the model with the server.</p>

        
          <div class='highlight'><pre>    <span class="attribute">noSyncSave</span>:<span class="function"> =&gt;</span>
        <span class="property">@generateId</span>() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@id</span>? <span class="keyword">or</span> <span class="property">@id</span> <span class="keyword">is</span> <span class="string">""</span></pre></div>
        
      
        
        <p>To override the &quot;destroy&quot; method whenever we need to trigger the destroy
without syncing anything with the server.</p>

        
          <div class='highlight'><pre>    <span class="attribute">noSyncDestroy</span>: <span class="function"><span class="params">(options)</span> =&gt;</span>
        <span class="property">@trigger</span> <span class="string">"destroy"</span>, <span class="keyword">this</span>, <span class="property">@collection</span>, options</pre></div>
        
      
        
        <h2 id="base-collection">BASE COLLECTION</h2>

        
      
        
        <p>This is the base abstract collection for all collections of the System App.</p>

        
          <div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">SystemApp</span>.<span class="title">BaseCollection</span> <span class="keyword">extends</span> <span class="title">Backbone</span>.<span class="title">Collection</span></span>

    <span class="attribute">typeName</span>: <span class="literal">null</span></pre></div>
        
      
        
        <p>When was it last saved?</p>

        
          <div class='highlight'><pre>    <span class="attribute">lastSave</span>: <span class="keyword">new</span> Date(<span class="number">2000</span>, <span class="number">1</span>, <span class="number">1</span>)</pre></div>
        
      
        
        <p>If collection is syncing with server, property fetching will be true.
Used to delay binding data to views while still loading.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetching</span>: <span class="literal">false</span></pre></div>
        
      
        
        <p>Temporary function to be executed whenever <code>fetch</code> is finished.
The callback must be in the format (err, results), whereas <code>err</code> contains
errors and problems (if any!) and <code>results</code> the final result.
IMPORTANT: this property will always cleared after called!!!</p>

        
          <div class='highlight'><pre>    <span class="attribute">onFetchCallback</span>: <span class="literal">null</span></pre></div>
        
      
        
        <h2 id="id-helpers">ID HELPERS</h2>

        
      
        
        <p>Return the collection&#39;s ID to be used on the local storage.</p>

        
          <div class='highlight'><pre>    <span class="attribute">localId</span>:<span class="function"> =&gt;</span>
        result = <span class="property">@typeName</span>
        result += <span class="property">@id</span> <span class="keyword">if</span> <span class="property">@id</span>?
        <span class="keyword">return</span> result</pre></div>
        
      
        
        <h2 id="fetch-data">FETCH DATA</h2>

        
      
        
        <p>Override backbone&#39;s <code>fetch</code>. Set <code>fetching</code> to true before
fetching new data from server, and cancel fetching if collection
is being fetched already.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetch</span>: <span class="function"><span class="params">(options)</span> =&gt;</span>
        <span class="keyword">return</span> <span class="keyword">if</span> <span class="property">@fetching</span>
        <span class="property">@fetching</span> = <span class="literal">true</span></pre></div>
        
      
        
        <p>If no options are passed then create one, and set the
<code>success</code> and <code>error</code> callbacks.</p>

        
          <div class='highlight'><pre>        options = {} <span class="keyword">if</span> <span class="keyword">not</span> options?
        options.success = <span class="property">@fetchSuccess</span>
        options.error = <span class="property">@fetchError</span>

        <span class="keyword">if</span> options.isLocal
            <span class="property">@fetchLocal</span> options
        <span class="keyword">else</span>
            Backbone.Collection.prototype.fetch.call <span class="keyword">this</span>, options</pre></div>
        
      
        
        <p>Fetch data from local storage, using jquery.localData plugin.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetchLocal</span>: <span class="function"><span class="params">(options)</span> =&gt;</span>
        <span class="property">@fetching</span> = <span class="literal">true</span>

        options = {} <span class="keyword">if</span> <span class="keyword">not</span> options?
        options.isLocal = <span class="literal">true</span></pre></div>
        
      
        
        <p>Get local data.</p>

        
          <div class='highlight'><pre>        localObj = $.localData.get <span class="property">@localId</span>()
        <span class="property">@fetching</span> = <span class="literal">false</span></pre></div>
        
      
        
        <p>If local object exists and is parsed successfully, trigger <code>fetchSuccess</code>. # If failed, trigger <code>fetchError</code>.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> localObj?
            <span class="keyword">if</span> options.reset
                <span class="property">@reset</span> <span class="property">@parse</span>(localObj, options), options
            <span class="keyword">else</span>
                <span class="property">@set</span> <span class="property">@parse</span>(localObj, options), options

            <span class="keyword">if</span> options.error
                <span class="property">@trigger</span> <span class="string">"error"</span>, <span class="keyword">this</span>, localObj, options</pre></div>
        
      
        
        <p>When <code>fetch</code> is successful, set <code>fetching</code> to false.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetchSuccess</span>:<span class="function"> =&gt;</span>
        <span class="property">@fetching</span> = <span class="literal">false</span></pre></div>
        
      
        
        <p>If <code>onFetchCallback</code> is defined, call and set it back to null.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> <span class="property">@onFetchCallback</span>?
            <span class="property">@onFetchCallback</span> <span class="literal">null</span>, <span class="literal">true</span>
            <span class="property">@onFetchCallback</span> = <span class="literal">null</span></pre></div>
        
      
        
        <p>When <code>fetch</code> has problems or throws an error, set <code>fetching</code> to false
and log to the console if debugging is enabled.</p>

        
          <div class='highlight'><pre>    <span class="attribute">fetchError</span>:<span class="function"> =&gt;</span>
        <span class="property">@fetching</span> = <span class="literal">false</span></pre></div>
        
      
        
        <p>If <code>onFetchCallback</code> is defined, call and set it back to null.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> <span class="property">@onFetchCallback</span>?
            <span class="property">@onFetchCallback</span> <span class="string">"Could not load from remote server."</span>, <span class="literal">null</span>
            <span class="property">@onFetchCallback</span> = <span class="literal">null</span>

        SystemApp.consoleLog <span class="property">@typeName</span> + <span class="string">".fetch"</span>, <span class="string">"Fetch ERROR: "</span> + <span class="property">@id</span>, arguments</pre></div>
        
      
        
        <p>Delete collection data from local storage.</p>

        
          <div class='highlight'><pre>    <span class="attribute">deleteLocal</span>:<span class="function"> =&gt;</span>
        $.localData.del <span class="property">@localId</span>()</pre></div>
        
      
        
        <h2 id="clear-data">CLEAR DATA</h2>

        
      
        
        <p>Clear all models by calling each model&#39;s destroy method,
and save the parent model if there&#39;s one.</p>

        
          <div class='highlight'><pre>    <span class="attribute">clear</span>:<span class="function"> =&gt;</span>
        _.each <span class="property">@models</span>, <span class="function"><span class="params">(item)</span> -&gt;</span> item.destroy() <span class="keyword">if</span> item?
        <span class="property">@parentModel</span>.save() <span class="keyword">if</span> <span class="property">@parentModel</span>?</pre></div>
        
      
        
        <h2 id="no-syncing-methods">NO SYNCING METHODS</h2>

        
      
        
        <p>To override the default &quot;create&quot; method whenever we don&#39;t need to sync
the collection models directly with the server.</p>

        
          <div class='highlight'><pre>    <span class="attribute">noSyncCreate</span>: <span class="function"><span class="params">(model, options)</span> =&gt;</span>
        options = (<span class="keyword">if</span> options <span class="keyword">then</span> _.clone(options) <span class="keyword">else</span> {})

        model = <span class="property">@_prepareModel</span>(model, options)
        <span class="keyword">return</span> <span class="literal">false</span> <span class="keyword">if</span> <span class="keyword">not</span> model? <span class="keyword">or</span> model <span class="keyword">is</span> <span class="literal">false</span>
        model.generateId() <span class="keyword">if</span> <span class="keyword">not</span> model.id? <span class="keyword">or</span> model.id <span class="keyword">is</span> <span class="string">""</span></pre></div>
        
      
        
        <p>Add the model without calling save afterwards.</p>

        
          <div class='highlight'><pre>        <span class="property">@add</span> model, options

        <span class="keyword">return</span> model</pre></div>
        
      
        
        <h2 id="extra-helpers">EXTRA HELPERS</h2>

        
      
        
        <p>Return an array containing all models which were changed since last sync.</p>

        
          <div class='highlight'><pre>    <span class="attribute">changedModels</span>:<span class="function"> =&gt;</span>
        result = []
        <span class="keyword">for</span> m <span class="keyword">in</span> <span class="property">@models</span>
            <span class="keyword">if</span> m.hasChanged()
                result.push m
        <span class="keyword">return</span> result</pre></div>
        
      
        
        <p>Get a specific model by its friendlyId. If no item is found, return null.</p>

        
          <div class='highlight'><pre>    <span class="attribute">getByFriendlyId</span>: <span class="function"><span class="params">(value)</span> =&gt;</span>
        result = <span class="property">@where</span> {<span class="attribute">friendlyId</span>: value}

        <span class="keyword">if</span> result.length &gt; <span class="number">0</span>
            <span class="keyword">return</span> result[<span class="number">0</span>]
        <span class="keyword">else</span>
            <span class="keyword">return</span> <span class="literal">null</span></pre></div>
        
      
      <div class="fleur">h</div>
    </div>
  </div>
</body>
</html>
